/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package validator;

import java.util.ResourceBundle;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.FacesValidator;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import org.primefaces.model.UploadedFile;
import utility.ExceptionThrower;
import utility.ResourceBundleLoader;

/**
 *
 * @author Carmela
 */
@FacesValidator(value="fotoValidator")
public class FotoValidator implements Validator{

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if(value instanceof UploadedFile){
            UploadedFile uf=(UploadedFile) value;
            ResourceBundle rb=ResourceBundleLoader.getResourceBundle(context);
            if(!uf.getFileName().matches("([^\\s]+(\\.(?i)(jp?g|png|gif))$)")){
                //ExceptionThrower.throwValidationException(context, rb, "fotoFormatM");
                ExceptionThrower.throwMessageError(context, rb, "fotoFormatM");
            }
            if(uf.getSize()>1048576L){
                //ExceptionThrower.throwValidationException(context, rb, "fotoDimensioneM");
                ExceptionThrower.throwMessageError(context, rb, "fotoDimensioneM");
            }
        }else{
            System.out.println("Non è istanza di uploaded file");
        }
    }
    
}
